//
//  LotteryOpenListViewController.m
//  PlayBall
//
//  Created by Huawen Liu on 12-7-16.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//


#import "JddLotteryOpenListViewController.h"
#import "JddCommonFunction.h"
#import "JddViewControllerManager.h"
#import "SBJson.h"


@interface JddLotteryOpenListViewController ()

@end

@implementation JddLotteryOpenListViewController

@synthesize listTable;
@synthesize issueId;
@synthesize code;
@synthesize type;
@synthesize issue;

SHARED_VIEW_CONTROLLER_IMPLEMENT(JddLotteryOpenListViewController);

-(void)openDetailButtonClicked:(NSString*)issueValue issueID:(NSString*)issueID{
    //11选5，11运夺金，重庆时时彩
    if ([type intValue] == LOTTERY_TYPE_ID_11S5 || [type intValue] == LOTTERY_TYPE_ID_11YDJ || [type intValue] == LOTTERY_TYPE_ID_CQSS) {
        
    }else{
    issueId = issueID;
    issue = issueValue;
    [JddCommonFunction setWaitingViewHidden:NO fromController:self];
    JddQueryLotteryrealDetailOperation *operation = [[JddQueryLotteryrealDetailOperation alloc] init];
    operation.IssueID = [NSNumber numberWithInt:[issueID intValue]];
    operation.LotID = [NSNumber numberWithInt:[type intValue]];
    operation.delegate = self;
    [[JddGlobalParameter sharedInstance].operationQueue addOperation:operation];
    }
}

-(void)QueryLotteryrealDetailOperationResult:(NSData *)result{
    [JddCommonFunction setWaitingViewHidden:YES fromController:self];
    if (result) {
        NSString *realResult = [JddDataParser parseBuyHistoryDetailResult:result];
        if(([type intValue] == 1)){
            [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_LOTTERY_OPEN_DETAIL_BD
                                                                 fromController:self
                                                                     withObject:realResult
                                                                    withObject2:issue];
        }else {
        [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_LOTTERY_OPEN_DETAIL 
                                                             fromController:self 
                                                                 withObject:realResult 
                                                                withObject2:issue];
        }
        
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70.0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [arrayResult count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    JddLotteryOpenListItemCell *cell = [JddLotteryOpenListItemCell getInstanceView];
    NSDictionary *cellData = [arrayResult objectAtIndex:indexPath.row];
    NSString *string3 = [cellData objectForKey:@"WinNumber"];
    cell.lotID = [NSNumber numberWithInt:[type intValue]];
    NSArray *array2 = [string3 componentsSeparatedByString:@"+"];
    NSMutableString *string4 = [array2 objectAtIndex:0];
    if (indexPath.row ==0 && [type intValue]!= 1) {
        cell.button1.hidden = NO;
        cell.button2.hidden = NO;
        cell.button3.hidden = NO;
        cell.button4.hidden = NO;
        cell.button5.hidden = NO;
        cell.button6.hidden = NO;
        cell.button7.hidden = NO;
        cell.button8.hidden = NO;
        
        switch ([type intValue]) {
            case 5:
            {
                cell.button8.hidden = YES;
                NSMutableString *string5 = [array2 objectAtIndex:1];
                [cell.button7 setBackgroundImage:[UIImage imageNamed:@"jdd_bulebollselected"] forState:UIControlStateNormal];
                NSArray *array3 = [string4 componentsSeparatedByString:@" "];
                NSArray *array4 = [string5 componentsSeparatedByString:@" "];
                [cell.button1 setTitle:[array3 objectAtIndex:0] forState:UIControlStateNormal];
                [cell.button2 setTitle:[array3 objectAtIndex:1] forState:UIControlStateNormal];
                [cell.button3 setTitle:[array3 objectAtIndex:2] forState:UIControlStateNormal];
                [cell.button4 setTitle:[array3 objectAtIndex:3] forState:UIControlStateNormal];
                [cell.button5 setTitle:[array3 objectAtIndex:4] forState:UIControlStateNormal];
                [cell.button6 setTitle:[array3 objectAtIndex:5] forState:UIControlStateNormal];
                [cell.button7 setTitle:[array4 objectAtIndex:1] forState:UIControlStateNormal];
            }
                break;
            case 39:{
                cell.lotID = [NSNumber numberWithInt:[type intValue]];
                
                cell.button8.hidden = YES;
                NSMutableString *string6 = [array2 objectAtIndex:1];
                [cell.button7 setBackgroundImage:[UIImage imageNamed:@"jdd_bulebollselected"] forState:UIControlStateNormal];
                [cell.button6 setBackgroundImage:[UIImage imageNamed:@"jdd_bulebollselected"] forState:UIControlStateNormal];
                NSArray *array13 = [string4 componentsSeparatedByString:@" "];
                NSArray *array14 = [string6 componentsSeparatedByString:@" "];
                [cell.button1 setTitle:[array13 objectAtIndex:0] forState:UIControlStateNormal];
                [cell.button2 setTitle:[array13 objectAtIndex:1] forState:UIControlStateNormal];
                [cell.button3 setTitle:[array13 objectAtIndex:2] forState:UIControlStateNormal];
                [cell.button4 setTitle:[array13 objectAtIndex:3] forState:UIControlStateNormal];
                [cell.button5 setTitle:[array13 objectAtIndex:4] forState:UIControlStateNormal];
                [cell.button6 setTitle:[array14 objectAtIndex:1] forState:UIControlStateNormal];
                [cell.button7 setTitle:[array14 objectAtIndex:2] forState:UIControlStateNormal];}
                break;
            case 70:{
                cell.button8.hidden = YES;
                cell.button7.hidden = YES;
                cell.button6.hidden = YES;
                cell.jiantouView.hidden = YES;
                cell.lotID = [NSNumber numberWithInt:[type intValue]];
                NSArray *array33 = [string4 componentsSeparatedByString:@" "];
                [cell.button1 setTitle:[array33 objectAtIndex:0] forState:UIControlStateNormal];
                [cell.button2 setTitle:[array33 objectAtIndex:1] forState:UIControlStateNormal];
                [cell.button3 setTitle:[array33 objectAtIndex:2] forState:UIControlStateNormal];
                [cell.button4 setTitle:[array33 objectAtIndex:3] forState:UIControlStateNormal];
                [cell.button5 setTitle:[array33 objectAtIndex:4] forState:UIControlStateNormal];}
                break;
            case LOTTERY_TYPE_ID_11YDJ:{
                cell.button8.hidden = YES;
                cell.button7.hidden = YES;
                cell.button6.hidden = YES;
                cell.jiantouView.hidden = YES;
                cell.lotID = [NSNumber numberWithInt:[type intValue]];
                NSArray *array33 = [string4 componentsSeparatedByString:@" "];
                [cell.button1 setTitle:[array33 objectAtIndex:0] forState:UIControlStateNormal];
                [cell.button2 setTitle:[array33 objectAtIndex:1] forState:UIControlStateNormal];
                [cell.button3 setTitle:[array33 objectAtIndex:2] forState:UIControlStateNormal];
                [cell.button4 setTitle:[array33 objectAtIndex:3] forState:UIControlStateNormal];
                [cell.button5 setTitle:[array33 objectAtIndex:4] forState:UIControlStateNormal];}
                break;
            case LOTTERY_TYPE_ID_CQSS:{
                cell.button8.hidden = YES;
                cell.button7.hidden = YES;
                cell.button6.hidden = YES;
                cell.jiantouView.hidden = YES;
                cell.lotID = [NSNumber numberWithInt:[type intValue]];
                
//                if (string4 != nil || string4.length != 0) {
//                    [cell.button1 setTitle:[NSString stringWithFormat:@"%@", [string4 substringWithRange:NSMakeRange(0, 1)]] forState:UIControlStateNormal];
//                    [cell.button2 setTitle:[NSString stringWithFormat:@"%@", [string4 substringWithRange:NSMakeRange(1, 1)]] forState:UIControlStateNormal];
//                    [cell.button3 setTitle:[NSString stringWithFormat:@"%@", [string4 substringWithRange:NSMakeRange(2, 1)]] forState:UIControlStateNormal];
//                    [cell.button4 setTitle:[NSString stringWithFormat:@"%@", [string4 substringWithRange:NSMakeRange(3, 1)]] forState:UIControlStateNormal];
//                    [cell.button5 setTitle:[NSString stringWithFormat:@"%@", [string4 substringWithRange:NSMakeRange(4, 1)]] forState:UIControlStateNormal];
//                    [cell.button1 setEnabled:true];
//                    [cell.button2 setEnabled:true];
//                    [cell.button3 setEnabled:true];
//                    [cell.button4 setEnabled:true];
//                    [cell.button5 setEnabled:true];
//                    
//                }

                NSArray *array33 = [string4 componentsSeparatedByString:@","];
                if (array33.count > 0) {
                    [cell.button1 setTitle:[array33 objectAtIndex:0] forState:UIControlStateNormal];
                }
                if (array33.count > 1) {
                    [cell.button2 setTitle:[array33 objectAtIndex:1] forState:UIControlStateNormal];
                }
                if (array33.count > 2) {
                    [cell.button3 setTitle:[array33 objectAtIndex:2] forState:UIControlStateNormal];
                }
                if (array33.count > 3) {
                    [cell.button4 setTitle:[array33 objectAtIndex:3] forState:UIControlStateNormal];
                }
                if (array33.count > 4) {
                    [cell.button5 setTitle:[array33 objectAtIndex:4] forState:UIControlStateNormal];
                }
            }
                break;
            case 6:{
                cell.button6.hidden =YES;
                cell.button7.hidden = YES;
                cell.button8.hidden = YES;
                cell.button4.hidden = YES;
                cell.button5.hidden = YES;
                [cell.button1 setTitle:[NSString stringWithFormat:@"%@", [string3 substringWithRange:NSMakeRange(0, 1)]] forState:UIControlStateNormal];
                [cell.button2 setTitle:[NSString stringWithFormat:@"%@", [string3 substringWithRange:NSMakeRange(1, 1)]] forState:UIControlStateNormal];
                [cell.button3 setTitle:[NSString stringWithFormat:@"%@", [string3 substringWithRange:NSMakeRange(2, 1)]] forState:UIControlStateNormal];}
                break;
            case 3:{
                cell.button8.hidden = YES;
                [cell.button1 setTitle:[NSString stringWithFormat:@"%@", [string3 substringWithRange:NSMakeRange(0, 1)]] forState:UIControlStateNormal];
                [cell.button2 setTitle:[NSString stringWithFormat:@"%@", [string3 substringWithRange:NSMakeRange(1, 1)]] forState:UIControlStateNormal];
                [cell.button3 setTitle:[NSString stringWithFormat:@"%@", [string3 substringWithRange:NSMakeRange(2, 1)]] forState:UIControlStateNormal];
                [cell.button4 setTitle:[NSString stringWithFormat:@"%@", [string3 substringWithRange:NSMakeRange(3, 1)]] forState:UIControlStateNormal];
                [cell.button5 setTitle:[NSString stringWithFormat:@"%@", [string3 substringWithRange:NSMakeRange(4, 1)]] forState:UIControlStateNormal];
                [cell.button6 setTitle:[NSString stringWithFormat:@"%@", [string3 substringWithRange:NSMakeRange(5, 1)]] forState:UIControlStateNormal];
                [cell.button7 setTitle:[NSString stringWithFormat:@"%@", [string3 substringWithRange:NSMakeRange(6, 1)]] forState:UIControlStateNormal];}
                break;
            case 63:{
                cell.button6.hidden =YES;
                cell.button7.hidden = YES;
                cell.button8.hidden = YES;
                cell.button4.hidden = YES;
                cell.button5.hidden = YES;
                [cell.button1 setTitle:[NSString stringWithFormat:@"%@", [string3 substringWithRange:NSMakeRange(0, 1)]] forState:UIControlStateNormal];
                [cell.button2 setTitle:[NSString stringWithFormat:@"%@", [string3 substringWithRange:NSMakeRange(1, 1)]] forState:UIControlStateNormal];
                [cell.button3 setTitle:[NSString stringWithFormat:@"%@", [string3 substringWithRange:NSMakeRange(2, 1)]] forState:UIControlStateNormal];}
                break;
            case 64:{
                cell.button6.hidden =YES;
                cell.button7.hidden = YES;
                cell.button8.hidden = YES;
                [cell.button1 setTitle:[NSString stringWithFormat:@"%@", [string3 substringWithRange:NSMakeRange(0, 1)]] forState:UIControlStateNormal];
                [cell.button2 setTitle:[NSString stringWithFormat:@"%@", [string3 substringWithRange:NSMakeRange(1, 1)]] forState:UIControlStateNormal];
                [cell.button3 setTitle:[NSString stringWithFormat:@"%@", [string3 substringWithRange:NSMakeRange(2, 1)]] forState:UIControlStateNormal];
                [cell.button4 setTitle:[NSString stringWithFormat:@"%@", [string3 substringWithRange:NSMakeRange(3, 1)]] forState:UIControlStateNormal];
                [cell.button5 setTitle:[NSString stringWithFormat:@"%@", [string3 substringWithRange:NSMakeRange(4, 1)]] forState:UIControlStateNormal];}
                
                break;
            case 13:{
                cell.lotID = [NSNumber numberWithInt:[type intValue]];
                NSMutableString *string16 = [array2 objectAtIndex:1];
                [cell.button8 setBackgroundImage:[UIImage imageNamed:@"jdd_bulebollselected"] forState:UIControlStateNormal];
                
                NSArray *array23 = [string4 componentsSeparatedByString:@" "];
                NSArray *array24 = [string16 componentsSeparatedByString:@" "];
                [cell.button1 setTitle:[array23 objectAtIndex:0] forState:UIControlStateNormal];
                [cell.button2 setTitle:[array23 objectAtIndex:1] forState:UIControlStateNormal];
                [cell.button3 setTitle:[array23 objectAtIndex:2] forState:UIControlStateNormal];
                [cell.button4 setTitle:[array23 objectAtIndex:3] forState:UIControlStateNormal];
                [cell.button5 setTitle:[array23 objectAtIndex:4] forState:UIControlStateNormal];
                [cell.button6 setTitle:[array23 objectAtIndex:5] forState:UIControlStateNormal];
                [cell.button7 setTitle:[array23 objectAtIndex:6] forState:UIControlStateNormal];
                [cell.button8 setTitle:[array24 objectAtIndex:1] forState:UIControlStateNormal];}
                break;
            case 9:{
                cell.button8.hidden = YES;
                cell.button7.hidden = YES;
                cell.button6.hidden = YES;
                cell.lotID = [NSNumber numberWithInt:[type intValue]];
                NSArray *array43 = [string4 componentsSeparatedByString:@" "];
                [cell.button1 setTitle:[array43 objectAtIndex:0] forState:UIControlStateNormal];
                [cell.button2 setTitle:[array43 objectAtIndex:1] forState:UIControlStateNormal];
                [cell.button3 setTitle:[array43 objectAtIndex:2] forState:UIControlStateNormal];
                [cell.button4 setTitle:[array43 objectAtIndex:3] forState:UIControlStateNormal];
                [cell.button5 setTitle:[array43 objectAtIndex:4] forState:UIControlStateNormal];}
                break;
            default:
                break;

        }
    }else {
        
        cell.lotID = [NSNumber numberWithInt:[type intValue]];
        NSArray *array2 = [string3 componentsSeparatedByString:@"+"];
        NSMutableString *string4 = [array2 objectAtIndex:0];
    if ([array2 count]>1) {
        cell.winBallLabel.text = [NSString stringWithFormat:@"%@",string4];
        cell.winBallLabel2.text = [array2 objectAtIndex:1];
    }else {
        cell.winBallLabel.text = string4;
        cell.winBallLabel2.text = @"";
        }
        if ([type intValue] == 70 || [type intValue] ==LOTTERY_TYPE_ID_CQSS || [type intValue] == LOTTERY_TYPE_ID_11YDJ) {
            cell.jiantouView.hidden = YES;
        }
    }
    cell.issueLabel.text = [NSString stringWithFormat:@"%@期",[cellData objectForKey:@"Issue"]];
    [cell.kjLbl layoutToRightOf:cell.issueLabel AndSpace:5];
    cell.issue = [NSString stringWithFormat:@"%d",[[cellData objectForKey:@"Issue"] intValue]];
    cell.issueID =[NSString stringWithFormat:@"%d",[[cellData objectForKey:@"IssueID"] intValue]];
    
    cell.delegate = self;
    return cell;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)QueryLotteryOpenDetailOperationResult:(NSData *)result{
    [JddCommonFunction setWaitingViewHidden:YES fromController:self];
    if (result) {
        SBJsonParser *jsonParse = [[SBJsonParser alloc] init];
        NSString *realresult = [JddDataParser parseBuyHistoryDetailResult:result];
        arrayResult = [jsonParse objectWithString:realresult];
    }
    [listTable reloadData];
}

-(void)startQuaryOperation{
    [JddCommonFunction setWaitingViewHidden:NO fromController:self];
    JddQueryLotteryOpenDetailOperation *operation2 = [[JddQueryLotteryOpenDetailOperation alloc] init];
    operation2.delegate = self;
    operation2.LotID = [NSString stringWithFormat:@"%d", [type intValue]];
    [[JddGlobalParameter sharedInstance].operationQueue addOperation:operation2];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,0)];
    listTable.tableFooterView = v;
    [self.listTable setSeparatorColor:SeparatorLineColor];
    if (IOS7) {
        [self.listTable setSeparatorInset:UIEdgeInsetsZero];
    }
    
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"LotteryOpenList"];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"LotteryOpenList"];

        UIButton *titileBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        titileBtn.frame = CGRectMake(0, 0, 0, 40);
        titileBtn.backgroundColor = [UIColor clearColor];
        titileBtn.titleLabel.font = [UIFont systemFontOfSize:19];
        switch ([type intValue]){
            case 5:
                [titileBtn setTitle:PAGE_SHUANGSEQIU_OPEN_LIST forState:UIControlStateNormal];
                break;
            case 39:
                [titileBtn setTitle:PAGE_DALETOU_OPEN_LIST forState:UIControlStateNormal];
                break;
            case 70:
                [titileBtn setTitle:PAGE_SELECT_FIVE_OPEN_LIST forState:UIControlStateNormal];
                break;
            case 6:
                [titileBtn setTitle:@"福彩3D开奖列表" forState:UIControlStateNormal];
                break;
            case 3:
                [titileBtn setTitle:@"七星彩开奖列表" forState:UIControlStateNormal];
                break;
            case 62:
                [titileBtn setTitle:@"十一运夺金开奖列表" forState:UIControlStateNormal];
                break;
            case 28:
                [titileBtn setTitle:@"重庆时时彩开奖列表" forState:UIControlStateNormal];
                break;
            case 63:
                [titileBtn setTitle:@"排列三开奖列表" forState:UIControlStateNormal];
                break;
            case 64:
                 [titileBtn setTitle:@"排列五开奖列表" forState:UIControlStateNormal];
                break;
            case 13:
                [titileBtn setTitle:@"七乐彩开奖列表" forState:UIControlStateNormal];
                break;
            case 9:
                [titileBtn setTitle:@"22选5开奖列表" forState:
                    UIControlStateNormal];
                break;
            case 1:
                [titileBtn setTitle:@"胜负彩/任选九开奖列表" forState:
                 UIControlStateNormal];
                break;
            default:
                break;
        }
        [titileBtn sizeToFit];
        self.navigationItem.titleView = titileBtn;
        
//        //self.navigationItem.hidesBackButton = YES;


    arrayResult = [[NSMutableArray alloc]init];
//    [listTable reloadData];

    [self startQuaryOperation];
}


- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
